import types from "../types";

const state = {
  userInfo: {},
  isAdmin: false,
  nickname: '',
  username: '',
  continueFlag: false
};

const actions = {
  setUserInfo({ commit }, userInfo) {
    commit(types.SET_USER_INFO, userInfo);
  }
};

const mutations = {
  [types.SET_USER_INFO](state, userInfo) {
    state.userInfo = userInfo;
    state.nickname = userInfo.nickname;
    state.username = userInfo.username;
    state.isAdmin = userInfo.isAdmin;
    state.avatar = userInfo.avatar;
    state.continueFlag = userInfo.continueFlag ? userInfo.continueFlag : false;
  }
};

const getters = {
  userInfo: state => state.userInfo,
  showName: state => state.nickname || state.username,
  isAdmin: state => state.isAdmin || false,
  avatar: state => state.avatar,
  continueFlag: state => state.continueFlag
};

export default {
  namespaced: true,
  state,
  actions,
  mutations,
  getters
};
